<?php
/**
 * Created by PhpStorm.
 * User: chen
 * Date: 2016/1/14
 * Time: 16:26
 */
/**
 * 扫码商品入口
 * 商品二维码实际url：http://game.guangjixinxi.com/musihui/Crm/ConsumeGood/index?id=ID
 */
class ConsumeGoodAction extends Action{
    public function _initialize(){
        import('ORG.Util.Session');
    }
    /**
     * 公共弹框方法
     */
    public function simpleAlert($content, $url = 'javascript:history.go(-1);')
    {
        echo "<SCRIPT language=JavaScript>alert('$content');</SCRIPT>";
        echo ' ';
        exit;
    }


    public function index(){
        $id=trim(I('get.id'));
        if(empty($id)){
            $this->simpleAlert('缺失参数中奖记录id');
        }

        session::set('id',$id);
        $url=getBaseUrl().'/musihui/dropGood.php';
        redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx08981071f73cb701&redirect_uri=$url&response_type=code&scope=snsapi_base&state=123#wechat_redirect");
    }

    /**
     * 接收openid写入缓存
     */
    public function getInfo(){
        $openid=trim(I('get.openid'));
        session::set('openid',$openid);
        redirect(U('doDrop'));
    }
    /**
     *显示卡券信息和核销
     */
    public function doDrop(){
        $id=session::get('id');
        $openid=session::get('openid');

        // $adminInfo=D('Admin')->where(array('openid'=>$openid))->find();
        // if(empty($adminInfo)){
        //     $this->simpleAlert('非核销人员请勿扫码');
        // }

        $adminid = D('AdminScan')->where(array('openid'=>$openid))->select();
        if(empty($adminid)){
            $this->simpleAlert('非核销人员请勿扫码');
        }

        $resultInfo=D('AwardsResult')->where(array('id'=>$id))->find();
        if(empty($resultInfo)){
            $this->simpleAlert('未找到中奖记录');
        }
        if($resultInfo['accept']==1){
            $this->simpleAlert('不可重复核销');
        }


        //可以核销商品，进行核销
        $resultInfo['accept']=1;
        $resultInfo['consumetime']=time();
        $resultInfo['consumeshop']=D('Admin')->where(array('id'=>$adminid[0]['adminid']))->getField('shop');
        $resultInfo['consumetype']='scan';
        D('AwardsResult')->data($resultInfo)->save();
        $this->simpleAlert('核销成功');


    }

}